package com.duzq;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * 解析本地文件
 *
 * @author duzq
 * @date 2021-03-21 22:15:46
 */
public class ReaderFile {
    public static void main(String[] args) {
        try {
            System.out.println("========= 本地文件");
            // 获取文件
            String path = System.getProperty("user.dir") + "/exchangeRate.txt";
            File file = new File(path);

            // 读取文件
            Reader reader = new FileReader(file);

            // 实例化字符流操作对象
            BufferedReader bufferedReader = new BufferedReader(reader);

            // 逐行读取
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                // 通过.split("|") 截取字符串，得到想到的文本内容做后续业务处理
                System.out.println(line);
            }

            System.out.println("========= 远程文件");
            // 获取远端文件流
            URL url = new URL("https://xxxxxxx.xxxxxx.cn/upload/20210301/exchangeRate.txt");
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            InputStream inputStream = connection.getInputStream();

            // 文件流转换成字符流
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            BufferedReader remoteBufferedReader = new BufferedReader(inputStreamReader);

            // 逐行读取
            while ((line = remoteBufferedReader.readLine()) != null) {
                // 通过.split("|") 截取字符串，得到想到的文本内容做后续业务处理
                System.out.println(line);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
